//
//  MaterielDetailViewController.m
//  AmountBody
//
//  Created by smm on 2018/1/24.
//  Copyright © 2018年 RCMTM. All rights reserved.
//

#import "MaterielDetailViewController.h"
#import "XLCardSwitch.h"
@interface MaterielDetailViewController ()<XLCardSwitchDelegate>
@property (strong, nonatomic) XLCardSwitch *cardSwitch;
@property (strong, nonatomic) UIView * downView;
@property (strong, nonatomic) UIButton * buyButton;

@end

@implementation MaterielDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.public_headView.hidden = NO;
    self.public_headView.leftButton = [self leftButton];
    self.public_headView.titleLabel = [self titleLabel];
    [self loadUI];
    // Do any additional setup after loading the view.
}
- (void)loadUI {
    //设置卡片浏览器
    //初始化数据源
    
    _cardSwitch = [[XLCardSwitch alloc] init];
    _cardSwitch.frame = CGRectMake(0, TOPNAVIGATIONBAR_H, SCREEN_W, SCREEN_H - 64 - USERDEFINED_TABBAR_H);
    [_cardSwitch addCollectionView];
    NSMutableArray *items = [NSMutableArray new];
    for (int i=0; i<10; i++) {
        XLCardItem * item = [[XLCardItem alloc]init];
        item.imageName = @"wuliaoPhoto";
        item.title = [NSString stringWithFormat:@"物料%d",i];
        [items addObject:item];
    }
    _cardSwitch.items = items;
    _cardSwitch.delegate = self;
    //分页切换
    _cardSwitch.pagingEnabled = true;
    //设置初始位置，默认为0
    _cardSwitch.selectedIndex = 3;
    [self.view addSubview:_cardSwitch];
    _downView = [[UIView alloc]init];
    [_downView setBianKuangAndColor:TAB_BAR_COLOR];
    [self.view addSubview:_downView];
    _buyButton = [[UIButton alloc]init];
    [_downView addSubview:_buyButton];
    [_buyButton setTitle:@"立即购买" forState:UIControlStateNormal];
    [_buyButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _buyButton.backgroundColor = RGBColorAlpha(255, 66, 89, 1);
    _buyButton.titleLabel.font = Cell_Big_Font;
}
-(void)viewDidLayoutSubviews
{
//    [super viewDidLayoutSubviews];
    _cardSwitch.frame = CGRectMake(0, TOPNAVIGATIONBAR_H, SCREEN_W, SCREEN_H - TOPNAVIGATIONBAR_H - USERDEFINED_TABBAR_H);
  
    NSMutableArray *items = [NSMutableArray new];
    for (int i=0; i<10; i++) {
        XLCardItem * item = [[XLCardItem alloc]init];
        item.imageName = @"wuliaoPhoto";
        item.title = [NSString stringWithFormat:@"物料%d",i];
        [items addObject:item];
    }
    [_cardSwitch addCollectionView];
   
    _cardSwitch.items = items;
    _cardSwitch.selectedIndex = 3;
    
    [_downView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.equalTo(@(USERDEFINED_TABBAR_H));
    }];
    CGFloat buttonW = 100;
    [_buyButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.equalTo(_downView);
        make.width.equalTo(@(buttonW));
    }];
}
#pragma mark -
#pragma mark CardSwitchDelegate

- (void)XLCardSwitchDidSelectedAt:(NSInteger)index {
    NSLog(@"选中了：%zd",index);
    
    //更新背景图
    XLCardItem *item = _cardSwitch.items[index];
    
}


- (void)switchPrevious {
    
    NSInteger index = _cardSwitch.selectedIndex - 1;
    index = index < 0 ? 0 : index;
    [_cardSwitch switchToIndex:index animated:true];
}

- (void)switchNext {
    NSInteger index = _cardSwitch.selectedIndex + 1;
    index = index > _cardSwitch.items.count - 1 ? _cardSwitch.items.count - 1 : index;
    [_cardSwitch switchToIndex:index animated:true];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
